#include<bits/stdc++.h>
#define int long long
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define Pr pair<int,int>
#define MP(i,j) make_pair(i,j)
#define Fo(i,j) for(auto i:j)
#define FO(i,j,k) for(int i=j;i;i=k)
#define PB(i) push_back(i)
#define FFF(i,j,k) for(auto i=j;i!=k;++i)
#define For(i,j,k) for(long long i=j;i<=k;++i)
#define FoR(i,j,k) for(long long i=j;i<k;++i)
#define FOR(i,j,k) for(long long i=j;i>=k;--i)
using namespace std;
template <typename _Tp>void read(_Tp& first) {
    _Tp x = 0, f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
    first = x * f;
}
#define Mod 1000000007
#define N 1000005
struct Node{
    int fa,t;
    int ls,rs;
    int dep;
    int s,z;
}a[N],k[N];
int n,ans[N],fck[N];
void dfs_dep(int x,int d){
//cout<<x<<endl;
    a[x].dep=d;
    if(a[x].ls)dfs_dep(a[x].ls,d+1);
    if(a[x].rs)dfs_dep(a[x].rs,d+1);
    return ;
}
void dfs_ans(int x,int s){
    ans[x]+=a[x].dep;
    ans[x]+=a[x].s;
    ans[x]+=a[x].z;
    ans[x]+=s;
    if(!a[x].ls)dfs_ans(a[x].ls,s+a[x].z);
    if(!a[x].rs)dfs_ans(a[x].rs,s+a[x].z);
}
int c(int x){
    if(!a[a[x].fa].fa)return 0;
    if(a[a[x].fa].t==a[x].t)return 1;
    else return 2;
}
void v1(int x,bool k){
    Node t=a[x];
    a[a[x].fa].fa=x;
    if(!k)a[x].rs=a[x].fa,t.rs=a[a[x].fa].ls;
    else a[x].ls=a[x].fa,t.ls=a[a[x].fa].rs;
}
void v2(int x,bool k){
    Node t=a[x],f=a[a[x].fa],ff=a[a[a[x].fa].fa];
    a[a[a[x].fa].fa].fa=t.fa;
    a[x].fa=ff.fa;
    a[x].t=ff.t;

}
void dfs(int x){
    int opt=c(x);
    if(opt==0){
        Node t;
        a[x].s--;a[a[x].fa].s++;
        if(!a[x].t)a[a[x].ls].z--,a[a[a[x].fa].rs].z++,v1(x,0);
        else a[a[x].rs].z--,a[a[a[x].fa].ls].z++,v1(x,1);
        dfs_ans(x,0);
        return;
    }if(opt==1){
        a[x].s-=2;
        a[a[a[x].fa].fa].s+=2;
        if(!a[x].t){
            a[a[a[a[x].fa].fa].rs].z+=2;
            a[a[a[x].fa].rs].z++;
            a[a[x].ls].z-=2;
            a[a[x].rs].z-=1;
            v2(x,0);
        }
        dfs(x);
    }
}
signed main(){
	freopen("dream.in","r",stdin);
	freopen("dream.out","w",stdout);
	int fuck=0;
    read(n);
    For(i,1,n){
        int ls,rs;
        read(ls),read(rs);
        if(ls)a[i].ls=ls,a[ls].fa=i,a[ls].t=0;
        if(rs)a[i].rs=rs,a[rs].fa=i,a[rs].t=1,fuck=1;
    }
    dfs_dep(1,0);
//    if(!fuck){
    	For(i,1,n)fck[i]=(fck[i-1]+i)%Mod;
    	For(i,1,n)ans[i]=(fck[a[i].dep]+fck[n-1-a[i].dep])%Mod;
    	For(i,1,n)cout<<ans[i]<<endl;
//	}
    return 0;
}
/*
4
2 0
3 0
4 0
0 0
*/
